class BrnnProxy extends ResourceProxyBase {
    public static NAME: string = "BrnnProxy";
    public static proxy: BrnnProxy = null;
    public constructor() {
        super(BrnnProxy.NAME);
    }
    public static getProxy() {
        if (!BrnnProxy.proxy) {
            BrnnProxy.proxy = new BrnnProxy();
        }
        return BrnnProxy.proxy;
    }
    //登录成功
    public login(data: Object) {
        BrnnVO.VO = null;
        BrnnVO.getVo().lUserScore = GameVO.getVo().lUserScore;
    }
    //获取房间配置信息，获取完成后发送匹配玩家请求
    public getRoomInfo(data: Object) {
        BrnnVO.getVo().dwServerRule = data["dwServerRule"];
        BrnnVO.getVo().wChairCount = data["wChairCount"];
        BrnnVO.getVo().wServerType = data["wServerType"];
        BrnnVO.getVo().wTableCount = data["wTableCount"];
    }
    //用户进入广场 携带用户所有数据 3-100
    public userJoinSquare(data: Object) {
        let user = data["tagMobileUserInfoHead"];
        BrnnVO.getVo().userInfo[user.dwUserID] = user;;//广场所有人存入集合
        this.userJoinRoom();
    }
    //用户战绩信息刷新 包括金币 3-101
    public getUserInfo(data: Object) {
        let UserScore = data["UserScore"];
        let dwUserID = data["dwUserID"];
        let lScore = UserScore["lScore"];
        let user = BrnnVO.getVo().userInfo[dwUserID];
        if (user) {
            user.lScore = lScore;
        }
        this.userJoinRoom();
    }
    //用户状态变更 入座，离开 3-102
    public userStateModify(data: Object) {
        //用户状态发生改变从缓存的所有用户中找到该用户改变其座位
        let dwUserID = data["dwUserID"];
        let wTableID = data["tableId"];
        let wChairID = data["wChairID"];
        let cbUserStatus = data["cbUserStatus"];
        let user = BrnnVO.getVo().userInfo[dwUserID];
        if (user) {
            user.wTableID = wTableID;
            user.wChairID = wChairID;
            user.cbUserStatus = cbUserStatus;
        }
        this.userJoinRoom();
    }

    public userJoinRoom() {
        BrnnVO.getVo().initUserInof();
        //状态改变完毕后从所有玩家中找自己是否已入座，若自己入座，则找是否跟自己同桌的用户，将其显示到桌子对面
        let user = BrnnVO.getVo().userInfo[GameVO.getVo().dwUserID]
        if (user) {
            BrnnVO.getVo().wTableId = user.wTableID;
            BrnnVO.getVo().wChairId = user.wChairID;
            BrnnVO.getVo().status = user.cbUserStatus;
            BrnnVO.getVo().lUserScore = user.lScore;
        }
        //我入座找对手
        if (BrnnVO.getVo().wTableId != null && BrnnVO.getVo().wTableId != 65535) {
            BrnnVO.getVo().wTableId_up = BrnnVO.getVo().wTableId
            for (let i in BrnnVO.getVo().userInfo) {
                let user = BrnnVO.getVo().userInfo[i]
                if (user.wTableID == BrnnVO.getVo().wTableId &&
                    user.dwUserID != GameVO.getVo().dwUserID) {//跟我坐一桌的就是对手
                    BrnnVO.getVo().playerNumber++;
                }
            }
            game.AppFacade.instance.sendNotification(PanelNotify.BRNN.JOIN_ROOM);
        } else {
            if (BrnnVO.getVo().wTableId_up != null && BrnnVO.getVo().wTableId == 65535) {
                //上次有座位此次没座位说明被踢了
                SocketManager.closeSocket(InstructionsEnum.GAME_TYPE_NAME.BRNN);
            }

        }
    }
    //获取牌局编号
    public getSerialNumber(data: Object) {
        BrnnVO.getVo().gameNumbering = data["szSerialNumber"];
        game.AppFacade.instance.sendNotification(PanelNotify.BRNN.GET_SERIAL_NUMBER)
    }

    //游戏开始
    public gameStart(data: Object) {
        BrnnVO.getVo().initGameInfo()
        BrnnVO.getVo().game_state = 1;
        BrnnVO.getVo().gameNum++;
        BrnnVO.getVo().cbTimeLeave = data["cbTimeLeave"];       //剩余时间
        BrnnVO.getVo().wBankerUser = data["bankerPos"];       //庄家用户
        BrnnVO.getVo().lBankerScore = data["bankerMoney"];        //庄家金币
        //根据当前金额计算出这一盘最多能下多少金额
        if (InstructionsEnum.LEVEL_GAME == 1 || InstructionsEnum.LEVEL_GAME == 4 || InstructionsEnum.LEVEL_GAME == 10) {
            BrnnVO.getVo().allowlUserScore = BrnnVO.getVo().lUserScore * 0.25;//赔4倍
        } else {
            BrnnVO.getVo().allowlUserScore = BrnnVO.getVo().lUserScore * 0.1;//赔10倍
        }
        game.AppFacade.instance.sendNotification(PanelNotify.BRNN.START_GAME);
    }
    //下注返回
    public betReceive(data: Object) {
        let wChairID = data["wChairID"];       //用户位置
        let cbBetArea = data["cbJettonArea"];        //筹码区域
        let lBetScore = data["lJettonScore"];        //加注数目
        BrnnVO.getVo().betUser = wChairID;
        BrnnVO.getVo().betAdds = cbBetArea;
        BrnnVO.getVo().betNum = lBetScore;
        if (cbBetArea == 1) {
            BrnnVO.getVo().betNumAdds_0 += lBetScore
        }
        if (cbBetArea == 2) {
            BrnnVO.getVo().betNumAdds_1 += lBetScore
        }
        if (cbBetArea == 3) {
            BrnnVO.getVo().betNumAdds_2 += lBetScore
        }
        if (cbBetArea == 4) {
            BrnnVO.getVo().betNumAdds_3 += lBetScore
        }
        if (wChairID == BrnnVO.getVo().wChairId) {
            BrnnVO.getVo().game_state = 2
            BrnnVO.getVo().gameNum = 0;
            BrnnVO.getVo().lUserScore -= lBetScore;
            let user = BrnnVO.getVo().userInfo[GameVO.getVo().dwUserID];
            if (user) {
                user.lScore = BrnnVO.getVo().lUserScore;
            }
            BrnnVO.getVo().betNumAdds_my += lBetScore;
            if (cbBetArea == 1) {
                BrnnVO.getVo().betNumAdds_0_my += lBetScore
            }
            if (cbBetArea == 2) {
                BrnnVO.getVo().betNumAdds_1_my += lBetScore
            }
            if (cbBetArea == 3) {
                BrnnVO.getVo().betNumAdds_2_my += lBetScore
            }
            if (cbBetArea == 4) {
                BrnnVO.getVo().betNumAdds_3_my += lBetScore
            }
        }
        game.AppFacade.instance.sendNotification(PanelNotify.BRNN.BET_RECEIVE);
    }
    //游戏结束
    public gameOver(data: Object) {
        var lBankerTotallScore = data["lBankerTotallScore"];//庄家现分
        var szNickName = data["szNickName"]; //庄家名字
        var cbTableCardArray = data["cbTableCardArray"]//桌面扑克
        var bOxCard = data["bOxCard"]//扑克类型
        var cbCardIndex = data["cbCardIndex"] //牛排索引
        var lBankerAreaWinScore = data["lBankerAreaWinScore"]; //庄家各区域成绩
        var lBankerScore = data["lBankerScore"];//庄家得分
        var lUserScore = data["lUserScore"];//我的得分
        var cbTimeLeave = data["cbTimeLeave"] //剩余时间

        BrnnVO.getVo().cbTimeLeave = cbTimeLeave;
        // BrnnVO.getVo().cbTimeLeave += 6;//上面空闲时间6秒叠加到这里避免倒计时被改变两次

        BrnnVO.getVo().poker_arr = cbTableCardArray;
        BrnnVO.getVo().niuType_arr = bOxCard;
        BrnnVO.getVo().niuIndex_arr = cbCardIndex;
        BrnnVO.getVo().score_arr = lBankerAreaWinScore;
        BrnnVO.getVo().score_my = lUserScore;
        BrnnVO.getVo().score_zhuang = lBankerScore;

        //我的当前金币=我现在金币加上总下注加上输赢分
        BrnnVO.getVo().lUserScore += BrnnVO.getVo().betNumAdds_my += lUserScore
        let user = BrnnVO.getVo().userInfo[GameVO.getVo().dwUserID];
        if (user) {
            user.lScore = BrnnVO.getVo().lUserScore;
        }

        var obj = {
            bWinDaoMen: 1,
            bWinDuiMen: 1,
            bWinHuang: 1,
            bWinShunMen: 1
        }
        if (lBankerAreaWinScore[1] > 0) {
            obj.bWinShunMen = 0
        }
        if (lBankerAreaWinScore[2] > 0) {
            obj.bWinDuiMen = 0
        }
        if (lBankerAreaWinScore[3] > 0) {
            obj.bWinDaoMen = 0
        }
        if (lBankerAreaWinScore[4] > 0) {
            obj.bWinHuang = 0
        }
        if (BrnnVO.getVo().ludan != null) {
            BrnnVO.getVo().ludan.unshift(obj);
        }
        game.AppFacade.instance.sendNotification(PanelNotify.BRNN.GAME_OVER);
    }
    //100-101 中途加入
    public dataRecovery(data: Object) {
        if (RegDataBrnn.MainCMD_MsgId[100][101] == RegDataBrnn.gamePlay) {
            BrnnVO.getVo().initGameInfo()
            BrnnVO.getVo().cbTimeLeave = data["cbTimeLeave"] //剩余时间
            BrnnVO.getVo().cbGameStatus = data["cbGameStatus"] //游戏状态
            var lAllBet: Array<any> = data["lCellScore"]//所有区域已下注
            var lPlayFreeSocre: Array<any> = data["lUserJettonScore"] //玩家下注
            BrnnVO.getVo().lAreaLimitScore = data["lAreaLimitScore"]//区域下注限制
            BrnnVO.getVo().betNumAdds_0 = lAllBet[1]
            BrnnVO.getVo().betNumAdds_1 = lAllBet[2]
            BrnnVO.getVo().betNumAdds_2 = lAllBet[3]
            BrnnVO.getVo().betNumAdds_3 = lAllBet[4]
            BrnnVO.getVo().betNumAdds_0_my = lPlayFreeSocre[1]
            BrnnVO.getVo().betNumAdds_1_my = lPlayFreeSocre[2]
            BrnnVO.getVo().betNumAdds_2_my = lPlayFreeSocre[3]
            BrnnVO.getVo().betNumAdds_3_my = lPlayFreeSocre[4]
            BrnnVO.getVo().betNumAdds_my = 0;
            for (let i in lPlayFreeSocre) {
                BrnnVO.getVo().betNumAdds_my += lPlayFreeSocre[i];
            }
            //中途加入，计算还可以下多少钱，要先算出开局时可以下多少钱，再扣去这一局已经下了多少钱
            var money = (BrnnVO.getVo().lUserScore + BrnnVO.getVo().betNumAdds_my)//开局之前我有多少钱
            if (InstructionsEnum.LEVEL_GAME == 1 || InstructionsEnum.LEVEL_GAME == 4 || InstructionsEnum.LEVEL_GAME == 10) {
                BrnnVO.getVo().allowlUserScore = money * 0.25;
            } else {
                BrnnVO.getVo().allowlUserScore = money * 0.1;
            }
            //已经得到这一局最多下注量，然后扣去我已经下注量，就是我剩余还可以下多少
            BrnnVO.getVo().allowlUserScore -= BrnnVO.getVo().betNumAdds_my

            game.AppFacade.instance.sendNotification(PanelNotify.BRNN.DATA_RECOVERY);
        }
        if (RegDataBrnn.MainCMD_MsgId[100][101] == RegDataBrnn.gameFree) {
            BrnnVO.getVo().cbTimeLeave = data["cbTimeLeave"]
            BrnnVO.getVo().lAreaLimitScore = data["lAreaLimitScore"]//区域下注限制
            game.AppFacade.instance.sendNotification(PanelNotify.BRNN.WAIT_NEXT_GAME);
        }
    }
    //空闲等待
    public waitNextGame(data: Object) {
        BrnnVO.getVo().cbTimeLeave = data["cbTimeLeave"];       //剩余时间
        game.AppFacade.instance.sendNotification(PanelNotify.BRNN.WAIT_START_GAME);
    }
    //推送路单
    public receiveLuDan(data: Object) {
        var struGameRecord: Array<any> = data["struGameRecord"];
        for (var i = struGameRecord.length - 1; i >= 0; i--) {
            BrnnVO.getVo().ludan.push(struGameRecord[i])
        }
        game.AppFacade.instance.sendNotification(PanelNotify.BRNN.RECEIVE_LUDAN);
    }
    //推送场外人员
    public receiveUserList(data: Object) {
        var structUserList = data["structUserList"]
        BrnnVO.getVo().structUserList = structUserList;
        var obj: Object = {
            lTakeMinScore: BrnnVO.getVo().lUserScore,
            szCityName: GameVO.getVo().szNickName,
            wFaceID: GameVO.getVo().wFaceID
        }
        BrnnVO.getVo().structUserList.push(obj)
        game.AppFacade.instance.sendNotification(PanelNotify.BRNN.RECEIVE_USER_LIST);
    }
}
